Skip to content

Conversation

HanzHager
Copy link

Adding timeout handling for I2C in Wire according to previously defined solution on other platforms
Also reduced the default timeout from 1000ms till 25ms
Also created src directory as well as adding keywords.txt and library.properties so that the "Wire" part of the repository can be handled as a separate library

@CLAassistant
Copy link

CLAassistant commented Oct 8, 2025

CLA assistant check
All committers have signed the CLA.

@HanzHager
Copy link
Author

This should solve the issue #492

@HanzHager
Copy link
Author

The actions to reset the hardware in handleTimeout() is not well tested. It would be great if someone who knows better can take a look at it to see if it makes sense and if there should be changes.

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Oct 8, 2025
The new logic for the timeout loop in read_from() and write_to() had a bug. It is now fixed.
Adding timeout handling for I2C in Wire according to previously defined solution on other platforms
Also reduced the default timeout from 1000ms to 25ms
@HanzHager
Copy link
Author

Fixed a bug in timeout handling in read_from() and write_to() and made the commit cleaner

@HanzHager
Copy link
Author

After some more testing I conclude that I need even more testing to make sure it works as intended.
I think that in the current solution, the master may not be able to correctly handle when a device on the bus is stuck in clock stretching. This should most likely be fixed before ever releasing this pull request.

@per1234
Copy link
Collaborator

per1234 commented Oct 11, 2025

Thanks for your pull request, and for taking the time to provide an update on the situation @HanzHager!

I see that one of Arduino's developers also submitted a similar pull request: #494

It would be great if you would review that prior pull request and help us to understand whether your PR provides any advancements when compared to the other, and whether the other might also suffer from any of the potential problems you have identified in testing of this PR.

@HanzHager
Copy link
Author

Thanks for your pull request, and for taking the time to provide an update on the situation @HanzHager!

I see that one of Arduino's developers also submitted a similar pull request: #494

It would be great if you would review that prior pull request and help us to understand whether your PR provides any advancements when compared to the other, and whether the other might also suffer from any of the potential problems you have identified in testing of this PR.

I looked at this PR and it looks like it is using setTimeout() instead of setWireTimeout() for the implementation.
There is already a defined way to handle setting timeout for "Wire" and this includes a function called setWireTimeout() as well as other functions described in https://docs.arduino.cc/language-reference/en/functions/communication/wire/setWireTimeout/

I have tried to make a complete implementation that harmonizes best with this and the AVR implementation.
Unfortunately, I am not fully confident that I am handling the Renesas api properly and I have realized that there needs to be some considerations for timeout situations where some device on the bus is keeping the SCL line low as result of clock stretching or some fault. So, I guess I need more testing and potentially some help in reviewing and making additional testing my solution, before it is deployed.

I am quite new in making contributions like this, so I probably need quite some guidance to get it right.

@HanzHager
Copy link
Author

I just noticed that #494 have renamed the setTimeout() to setWireTimeout(). The function sets timeout in milliseconds, but the referred implementation https://docs.arduino.cc/language-reference/en/functions/communication/wire/setWireTimeout/ are using microseconds as well as a boolean second parameter that tells the implementation to reset the bus on timeout or not.
So, even if #494 is small and simple solution, it is not compatible with other implementations and the reference documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: code Related to content of the project itself type: enhancement Proposed improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I2C scan takes extremely long (~126 seconds) with Wire.endTransmission() on empty bus

3 participants